<?php

/**
 * Organization controller
 *
 * Organization controller
 *
 * PHP Version: 1.2.3
 *
 * @author Johari <joharijumali@gmail.com>
 */

/**
 * Admin Configuration class
 *
 * Admin Configuration class
 *
 * @author Johari <joharijumali@gmail.com>
 */

class Administrator_Organization_Controller extends Base_Controller
{

    public $restful = true;

    /**
     * Jabatan / Bahagian / Unit management function
     * Component : department
     * Method : ajax post
	 *
     * @return void
     **/
    public function get_department()
	{
        $data['listDeparment'] = Data_Department_Department::listDepartment();

        return View::make('administrator.organization.department', $data);
    }
	
	/**
     * Function post_department
	 *
	 * @return post_department.
     */
    public function post_department()
	{
        $input = Input::get();
		
        $rules = array(
            'wpj_jabatan_name' => 'required|unique:sys_user_jabatan,suj_jabatan_name',
        );
        
        $messages = array(
            'unique' => 'Jabatan Telah Wujud',
        );
        
        $validation = Validator::make($input, $rules, $messages);

        if ($validation->fails()) {
            return json_encode($validation->errors);
        }
														
		Data_Department_Department::addDepartment($input);
				
        return json_encode(Data_Department_Department::listDepartment());
    }
	
	public function post_editDepartment()
	{
        $input = Input::get();

        $rules = array(
            'wpj_jabatan_name' => 'required|unique:sys_user_jabatan,suj_jabatan_name,'.$input['wpj_no_auto'].',suj_no_auto',
        );
        
        $messages = array(
            'unique' => 'Jabatan Telah Wujud',
        );
		
		$validation = Validator::make($input, $rules, $messages);

		if ($validation->fails()) {
			return json_encode($validation->errors);
		}
														
		Data_Department_Department::editDepartment($input);
				
        return json_encode(Data_Department_Department::listDepartment());
    }
	
	/**
     * Function get_deptInfo
	 *
	 * @return get_deptInfo.
     */
    public function get_deptInfo()
	{
        return json_encode(Data_Department_Department::deptInfo(Input::get('id')));
    }

	/**
     * Function post_deleteDepartment
	 *
	 * @return post_deleteDepartment.
     */
	public function post_deleteDepartment()
	{
        $pass = Data_Department_Department::validateData(Input::get('id'));

        if ($pass) {
            Data_Department_Department::deleteDept(Input::get());

            return json_encode(Data_Department_Department::listDepartment());
        } else {
            //return json_encode(array('fail'=>Str::title(Lang::line('admin.deletedatafail')->get())));
            return json_encode(array('fail'=>Lang::line('admin.deletedatajabatan')->get()));
        }
	}

	/**
     * Bahagian
	 *
	 * @return get_division.
     */
    public function get_division()
	{
	    $data['wpj_no_auto'] = URI::segment(5);
        $data['header'] = Data_Department_Department::find(URI::segment(5))->wpj_jabatan_name;
        $data['listDivision'] = Data_Department_Division::listDivision();

        return View::make('administrator.organization.division', $data);
    }

	/**
     * Function post_division
	 *
	 * @return post_division.
     */
	public function post_division()
	{
        $input = Input::get();

        $rules = array(
            'wpb_bahagian_name' => 'required|uniquefor:sys_user_bahagian,sub_bahagian_name,'.$input['wpj_no_auto'].',suj_no_auto',
        );
        
        $messages = array(
            'uniquefor' => 'Bahagian Telah Wujud',
        );

        $validate = Validator::make($input, $rules,$messages);

        if ($validate->fails()) {
            return json_encode($validate->errors);
        }

        Data_Department_Division::addDivision($input);

        return json_encode(Data_Department_Division::listDivision());
	}
	
	public function post_editDivision()
	{
        $input = Input::get();
		//var_dump($input);die;
			
		$rules = array(
			'wpb_bahagian_name' => 'required|uniqueval:sys_user_bahagian,sub_bahagian_name,'.$input['wpb_no_auto'].',sub_no_auto,'.$input['wpj_no_auto'].',suj_no_auto',
		);
		
        $messages = array(
            'uniqueval' => 'Bahagian Telah Wujud',
        );

		$validation = Validator::make($input, $rules, $messages);

		if ($validation->fails()) {
		//var_dump($validation->errors);die;
			return json_encode($validation->errors);
		}
		
		Data_Department_Division::editDivision($input);
				
		return json_encode(Data_Department_Division::listDivision());
    }
	
	/**
     * Function get_divInfo
	 *
	 * @return get_divInfo.
     */
    public function get_divInfo()
	{
        return json_encode(Data_Department_Division::divInfo(Input::get('id')));
    }
	
	/**
     * Function post_deleteDivision
	 *
	 * @return post_deleteDivision.
     */
	public function post_deleteDivision()
	{
        $pass = Data_Department_Division::validateData(Input::get('id'));
		
        if ($pass) {
            Data_Department_Division::deleteDiv(Input::get());

            return json_encode(Data_Department_Division::listDivision());
            //return json_encode(Data_Department_Department::listDepartment());
        } else {
            //return json_encode(array('fail'=>Str::title(Lang::line('admin.deletedatafail')->get())));
            return json_encode(array('fail'=>Lang::line('admin.deletedatabahagian')->get()));
        }
	}

	/**
     * Unit
	 *
	 * @return get_unit.
     */
    public function get_unit()
	{
        $data['header'] = Str::upper(Data_Department_Department::find(URI::segment(5))->wpj_jabatan_name);
        $data['smallheader'] = '';
        $data['wpb_no_auto'] = null;

        if (URI::segment(6) != null) {
            $data['header'] = Str::upper(Data_Department_Division::find(URI::segment(6))->wpb_bahagian_name);
            $data['smallheader'] = Str::upper(Data_Department_Department::find(URI::segment(5))->wpj_jabatan_name);
            $data['wpb_no_auto'] = URI::segment(6);
        }
        
        $data['wpj_no_auto'] = URI::segment(5);
        $data['listUnit'] = Data_Department_Unit::listUnit();

        return View::make('administrator.organization.unit', $data);
    }

	/**
     * Function post_unit
	 *
	 * @return post_unit.
     */
	public function post_unit()
	{
        $input = Input::get();

        if($input['wpb_no_auto'] == null):
            $ival = $input['wpj_no_auto'];
            $ifield = 'suj_no_auto';
        else:
            $ival = $input['wpb_no_auto'];
            $ifield = 'sub_no_auto';
        endif;

        $rules = array(
            'wpu_unit_name' => 'required|uniquefor:sys_user_unit,suu_unit_name,'.$ival.','.$ifield,
        );
        
        $messages = array(
            'uniquefor' => 'Unit Telah Wujud',
        );

        $validate = Validator::make($input, $rules, $messages);

        if ($validate->fails()) {
            return json_encode($validate->errors);
        }

        Data_Department_Unit::addUnit($input);

        return json_encode(Data_Department_Unit::listUnit());
	}
	
	public function post_editUnit()
	{
        $input = Input::get();
		
		if($input['wpb_no_auto'] == 0):
            $ival = $input['wpj_no_auto'];
            $ifield = 'suj_no_auto';
        else:
            $ival = $input['wpb_no_auto'];
            $ifield = 'sub_no_auto';
        endif;
				
		$rules = array(
			'wpu_unit_name' => 'required|uniqueval:sys_user_unit,suu_unit_name,'.$input['wpu_no_auto'].',suu_no_auto,'.$ival.','.$ifield,
		);
		
        $messages = array(
            'uniqueval' => 'Unit Telah Wujud',
        );

		$validation = Validator::make($input, $rules, $messages);

		if ($validation->fails()) {
			return json_encode($validation->errors);
		}
														
		Data_Department_Unit::editUnit($input);
				
        return json_encode(Data_Department_Unit::listUnit());
    }

	/**
     * Function get_unitInfo
	 *
	 * @return get_unitInfo.
     */
    public function get_unitInfo()
	{
        return json_encode(Data_Department_Unit::unitInfo(Input::get('id')));
    }
	
	/**
     * Function post_deleteUnit
	 *
	 * @return post_deleteUnit.
     */
	public function post_deleteUnit()
	{
        $pass = Data_Department_Unit::validateData(Input::get('id'));

        if ($pass) {
            Data_Department_Unit::deleteUnit(Input::get());

            return json_encode(Data_Department_Unit::listUnit());
        } else {
            return json_encode(array('fail'=>Lang::line('admin.deletedataunit')->get()));
        }
	}

}

?>